package com.qingge.springboot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingge.springboot.entity.Stu;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingge.springboot.common.Result;

import com.qingge.springboot.service.ISubjectService;
import com.qingge.springboot.entity.Subject;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xyc
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private ISubjectService subjectService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Subject subject) {
        subjectService.saveOrUpdate(subject);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        subjectService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        subjectService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result findAll() {
        return Result.success(subjectService.list());
    }

    // 根据id查找对应学生的课程 flag 置为1
    @GetMapping("/setFlag")
    public Result setFlag(@RequestParam Integer id) {

        subjectService.clearFlag();

        List<Subject> lists = subjectService.findSubjectById(id);
        for (Subject s : lists) {
            subjectService.setFlagNew(s.getId());
        }
        return Result.success(subjectService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(subjectService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        return Result.success(subjectService.findPage(new Page<>(pageNum, pageSize)));
    }

    @GetMapping("/page2")
    public Result findPage2(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize) {
        return Result.success(subjectService.findPage2(new Page<>(pageNum, pageSize)));
    }

    //查询某个学生所选择的课程
    @GetMapping("/subjects")
    public Result findSubjectByStuName(@RequestParam String stuName) {
        return Result.success(subjectService.findSubjectByName(stuName));
    }

    //  在用户管理中 查看学生所选择的课程
    @GetMapping("/subjects2")
    public Result findSubjectByStuName2(@RequestParam Integer id) {  // 获取到user表中id

        String name = subjectService.findStuName(id);
        if (name != null) {
            return Result.success(subjectService.findSubjectByName(name));
        } else {
            return Result.error();
        }
    }

    // 根据id 查找课程       获取到stu表中的id
    @GetMapping("/subject3")
    public Result findSubjectById(@RequestParam Integer id) {
        return Result.success(subjectService.findSubjectById(id));
    }

    // 选课中设置 flag
    @GetMapping("/setSubjectFlag")
    public Result choose(@RequestParam String username) {
        subjectService.clearFlag();
        subjectService.chooseFlag(username);
        return Result.success();
    }

    //
    @GetMapping("/inputScore")
    public Result inputScore(@RequestParam Integer id) { // stuId
        List<Subject> lists = subjectService.inputScore(id);
        return Result.success(lists);
    }
}

